package com.itheima.blog.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.blog.util.UserThreadLocalUtils;
import com.itheima.blog.web.domain.dto.TBlogDto;
import com.itheima.blog.web.domain.po.TAlbum;
import com.itheima.blog.web.domain.po.TBlog;
import com.itheima.blog.web.domain.query.BlogPageQuery;
import com.itheima.blog.web.service.ITBlogService;
import com.itheima.common.domain.PageReqDto;
import com.itheima.common.domain.PageResDto;
import com.itheima.common.domain.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/web/owner")
@Api(value = "我的播客相关接口", tags = {"我的博客"})
@Slf4j
@RequiredArgsConstructor
public class OwnerController {

    private final ITBlogService itBlogService;
    @GetMapping("/blog/blogs")
    @ApiOperation("分页查询")
    public R<PageResDto> getBlogPages(@RequestBody BlogPageQuery blogPageQuery){
        PageResDto pageResDto = itBlogService.getBlogPages(blogPageQuery);
        return R.ok(pageResDto);
    }
    @PostMapping("/blog")
    @ApiOperation("新增Blog")
    public R add(@RequestBody TBlog tBlog) {
        itBlogService.add(tBlog);
        return R.ok();
    }

    @GetMapping("/blog/{blogId}")
    @ApiOperation("根据Id查询")
    public TBlog getById(@PathVariable Long blogId) {
        return itBlogService.getById(blogId);
    }
    @ApiOperation("根据ID修改")
    @PutMapping("/blog/{blogId}")
    public R update(@RequestBody TBlog tBlog, @PathVariable("blogId") Long blogId) {
        itBlogService.updateBlog(tBlog, blogId);
        return R.ok();
    }

    @ApiOperation("根据Id删除自己的数据，只能删除自己的")
    @DeleteMapping("/blog{/blogId}")
    public void delete(@PathVariable ("blogId") Long blogId) {
        itBlogService.deleteBlog(blogId);
    }

    @PostMapping("/blog/commentset")
    @ApiOperation("博客是否开启评论")
    public R changeBlogComment(@RequestBody TBlog comment){
        return itBlogService.changeBlogComment(comment)? R.ok() : R.error("修改失败");
    }

    @GetMapping("/album/user")
    @ApiOperation("根据用户id查询照片")
    public R getimgeById(){
    //itBlogService.list(UserThreadLocalUtils.getWebUserId())
        return R.ok(itBlogService.getImages());
    }

    @PutMapping("/blog/publish")
    @ApiOperation("文章发布/下架")
    public R chengePublish(@RequestBody TBlog tBlog){
        return itBlogService.chengePublish(tBlog)? R.ok() : R.error("修改失败");
    }
}
